package com.ccit.controller;

import com.ccit.domain.Approval;
import com.ccit.domain.PageBean;
import com.ccit.domain.Punch;
import com.ccit.service.ApprovalService;
import com.ccit.utils.Code;
import com.ccit.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/approvals")
@CrossOrigin(origins = "http://localhost:8080")
@Api(description = "审批管理")
public class ApprovalController {

    @Autowired
    private ApprovalService approvalService;

    @PostMapping
    @ApiOperation(value = "请假")
    public Result add(@RequestBody Approval approval) {
        boolean flag = approvalService.add(approval);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @PostMapping("/punch")
    @ApiOperation(value = "补打卡")
    public Result add(@RequestBody Punch punch) {
        boolean flag = approvalService.addPunch(punch);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @PutMapping
    @ApiOperation(value = "批改员工请假")
    public Result update(@RequestBody Approval approval) {
        boolean flag = approvalService.update(approval);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @PutMapping("/updatePunch")
    @ApiOperation(value = "批改员工补打卡")
    public Result update(@RequestBody Punch punch) {
        boolean flag = approvalService.update(punch);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = approvalService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取员工请假记录")
    public Result getById(@PathVariable Integer id) {
        List<Approval> approvalList  = approvalService.getById(id);
        Integer code = approvalList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = approvalList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,approvalList,msg);
    }


    @GetMapping("punch/{id}")
    @ApiOperation(value = "根据ID获取员工补卡记录")
    public Result getPunchById(@PathVariable Integer id) {
        List<Punch> punchList  = approvalService.getPunchById(id);
        Integer code = punchList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = punchList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,punchList,msg);
    }

    @GetMapping
    public Result selectAll(){
        List<Approval> approvalList = approvalService.selectAll();
        Integer code = approvalList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = approvalList != null ? "数据查询成功" : "数据查询失败，请重试！";
        return new Result(code,approvalList,msg);
    }

    @GetMapping("/page")
    @ApiOperation(value = "获取员工请假记录")
    public PageBean<Approval> getApprovals(
            @RequestParam("currentPage") int currentPage,
            @RequestParam("pageSize") int pageSize) {
        //2. 调用service查询
        PageBean<Approval> pageBean = approvalService.selectByPage(currentPage,pageSize);
//        System.out.println(pageBean);
        return  pageBean;
    }


    @GetMapping("/pagePunch")
    @ApiOperation(value = "获取员工补打卡记录")
    public PageBean<Punch> getPunch(
            @RequestParam("currentPage") int currentPage,
            @RequestParam("pageSize") int pageSize) {
        //2. 调用service查询
        PageBean<Punch> pageBean = approvalService.selectByPunchPage(currentPage,pageSize);
//        System.out.println(pageBean);
        return  pageBean;
    }

}
